<script src="<?= base_url()?>public/js/jquery.min.js"></script>

<div class="content-wrapper">
    <!-- 内容标题 -->
    <section class="content-header">
      <h1>
        <small>用户管理/</small> 编辑用户
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">用户管理</a></li>
        <li class="active">编辑用户</li>
      </ol>
    </section>

    <!-- 内容区 -->
    <form action="<?=base_url()?>index.php/admin/user/update/<?= $id?>" method="post" id='EditForm'>
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
              <div class="box">
                <div class="box-header with-border">
                   <h3 class="box-title">基础信息</h3>
                </div>
                <div class="box-body detail-page-sy"><br>
                    <dl class="dl-horizontal">
                      <dt>用户名：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <input type="text" name="user[name]" fld='name'
                            style='width:245px;' class="form-control"
                          value="<?php echo (isset($user['name'])?$user['name']:'') ?>">
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <dt>姓名：</dt>
                      <dd>
                         <div class="col-xs-4 row">
                            <input type="text" name="user[user_name]" fld='user_name'
                                style='width:245px;' class="form-control"
                            value="<?php echo (isset($user['user_name'])?$user['user_name']:'') ?>">
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <dt>邮箱：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <input type="text" name="user[email]" fld='email'
                                style='width:245px;' class="form-control"
                            value="<?php echo (isset($user['email'])?$user['email']:'') ?>">
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <dt>品牌：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <select name="user[brand_id]" fld='brand_id' style='width:245px;' class="form-control">
                                <option value="">请选择</option>
                                <?php
                                foreach ($brand_opts as $key => $opts) {
                                ?>
                                    <option value="<?=$opts['id'] ?>" <?php if(isset($user['brand_id']) && $user['brand_id'] == $opts['id']) echo 'selected';?>><?=$opts['name'] ?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <dt>权限组：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <select name="user[permis_id]" fld='permis_id' style='width:245px;' class="form-control">
                                <option value="">请选择</option>
                                <?php
                                foreach ($permis_opts as $key => $opts) {
                                ?>
                                    <option value="<?=$opts['id'] ?>" <?php if(isset($user['permis_id']) && $user['permis_id'] == $opts['id']) echo 'selected';?>><?=$opts['name'] ?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <dt>部门：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <select name="user[group_id]" fld='group_id' style='width:245px;' class="form-control">
                                <option value="">请选择</option>
                                <?php
                                foreach ($group_opts as $key => $opts) {
                                ?>
                                    <option value="<?=$opts['id'] ?>" <?php if(isset($user['group_id']) && $user['group_id'] == $opts['id']) echo 'selected';?>><?=$opts['name'] ?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div style='color:red;'></div>
                      </dd><br>
                      <?php if(1 == $current_id){?>
                      <dt>是否管理员：</dt>
                      <dd>
                        <div class="col-xs-4 row">
                            <select name="user[is_admin]" fld='is_admin' id='is_admin' style='width:245px;' class="form-control">
                                <?php
                                foreach ($is_admin_opts as $key => $opts) {
                                    $selected = '';
                                    if(isset($user['is_admin']) && $user['is_admin'] == $key){
                                        $selected = 'selected';
                                    }
                                ?>
                                    <option value="<?=$key ?>" <?=$selected?>><?=$opts['opts_val'] ?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div style='color:red;'></div>
                      </dd>
                    <?php }else{?>
                        <input type="hidden" id='is_admin' value='<?=$user['is_admin']?>'>
                    <?php }?>
                    </dl>
                </div>
                <!-- /.box-body -->
                <div class="box-footer align-center">
                    <button type="button" class="btn btn-primary btn-big bg-pink" id='submitBtn'>编辑</button>
                    <button type="button" class="btn btn-default btn-big" id='cancelBtn'>返回</button>
                </div>
              </div>
            </div>
            <!-- /.col -->
          </div>
    </section>
    </form>
</div>

<script>
$(function(){
    var is_submit = 1;
    $('#submitBtn').on('click',function(){
        var idx = 1;
        $('#EditForm input[fld=name]').val($.trim($('#EditForm input[fld=name]').val()));
        if($('#EditForm input[fld=name]').val() == ''){
            $('#EditForm input[fld=name]').parent().next().html('请输入用户名。');
            idx = 0;
        }else{
            $('#EditForm input[fld=name]').parent().next().html('');
        }
        if($('#EditForm input[fld=user_name]').val() == ''){
            $('#EditForm input[fld=user_name]').parent().next().html('请输入用户姓名。');
            idx = 0;
        }else{
            $('#EditForm input[fld=user_name]').parent().next().html('');
        }
        if(1 != $('#is_admin').val()){
            if($('#EditForm select[fld=brand_id]').val() == ''){
                $('#EditForm select[fld=brand_id]').parent().next().html('请选择品牌。');
                idx = 0;
            }else{
                $('#EditForm select[fld=brand_id]').parent().next().html('');
            }
            if($('#EditForm select[fld=permis_id]').val() == ''){
                $('#EditForm select[fld=permis_id]').parent().next().html('请选择权限组。');
                idx = 0;
            }else{
                $('#EditForm select[fld=permis_id]').parent().next().html('');
            }
        }
        if(0 == idx){
            return false;
        }
        if(1 != is_submit){
            return false;
        }
        is_submit = 2;
        $('#EditForm').submit();

    });
    $('#cancelBtn').on('click',function(){
        location.href = '../index';
    });
    var error_code = "<?= (isset($error_code)?$error_code:'')?>";
    if(error_code != ''){
        $('#EditForm input[fld=username]').parent().next().html(error_code);
    }
});
</script>
